package com.premedsidekick;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DefaultCoursesDatabaseAdapter {
	int DATABASE_VERSION = 1;
	String DATABASE_NAME = "course_requirements_table";
	String KEY_ROWID = "_id";
	String KEY_QUARTER_FLAG = "quarterflag";
	String KEY_FIRSTSEMESTER = "firstsemester";
	String KEY_SECONDSEMESTER = "secondsesmeter";
	String KEY_FIRSTQUARTER = "firstquarter";
	String KEY_SECONDQUARTER = "secondquarter";
	String KEY_THIRDQUARTER = "thirdquarter";
	String KEY_COMPLETE = "complete";
	Context context;
	SQLiteDatabase database;
	CourseRequirementsDatabaseHelper dbHelper;
	ContentValues values;
	String[] semester_list = {KEY_FIRSTSEMESTER, KEY_SECONDSEMESTER};
	String[] quarter_list = {KEY_FIRSTQUARTER, KEY_SECONDQUARTER, KEY_THIRDQUARTER};
	
	DefaultCoursesDatabaseAdapter(Context context) {
		this.context = context;
	}
	
	public DefaultCoursesDatabaseAdapter open() throws SQLException {
		dbHelper = new CourseRequirementsDatabaseHelper(context);
		database = dbHelper.getWritableDatabase();
		return this;
	}
	public void close() {
		dbHelper.close();
	}
	public void create() {
		for (int i = 0; i < 9; i++) {
			values = createInitialValues(i);
			database.insert(DATABASE_NAME, null, values);
		} 
	}
	public boolean updateQuarterFlag(long rowId, String flag) {
		values = new ContentValues();
		values.put(KEY_QUARTER_FLAG, flag);
		return database.update(DATABASE_NAME, values, KEY_ROWID + "=" + rowId, null) > 0;
	}
	public boolean updateSemester(long rowId, int position, String grade) {
		values = new ContentValues();
		System.out.println("Row id: " + rowId);
		System.out.println("Semester List: " + semester_list[position]);
		System.out.println("Grade: " + grade);
		values.put(semester_list[position], grade);
		return database.update(DATABASE_NAME, values, KEY_ROWID + "=" + rowId, null) > 0;
	}
	public boolean updateQuarter(long rowId, int position, String grade) {
		values = new ContentValues();
		values.put(quarter_list[position], grade);
		return database.update(DATABASE_NAME, values, KEY_ROWID + "=" + rowId, null) > 0;
	}
	/*
	public boolean updateSemester(long rowId, String first, String second) {
		values = createSemesterValues(first, second);
		return database.update(DATABASE_NAME, values, KEY_ROWID + "=" + rowId, null) > 0;
	}
	public boolean updateQuarter(long rowId, String first, String second, String third) {
		values = createQuarterValues(first, second, third);
		return database.update(DATABASE_NAME, values, KEY_ROWID + "=" + rowId, null) > 0;
	}
	*/
	public boolean delete() {
		return database.delete(DATABASE_NAME, null, null) > 0;
	}

	public Cursor getRow(long rowId) {
		return database.query(DATABASE_NAME, new String[] {KEY_ROWID, KEY_QUARTER_FLAG,
				KEY_FIRSTSEMESTER, KEY_SECONDSEMESTER, KEY_FIRSTQUARTER, KEY_SECONDQUARTER,
				KEY_THIRDQUARTER, KEY_COMPLETE}, KEY_ROWID + "=" + rowId, null, null, null, null);
	}
	public Cursor getSemester(long rowId) {
		return database.query(DATABASE_NAME, new String[] {KEY_FIRSTSEMESTER,
				KEY_SECONDSEMESTER}, KEY_ROWID + "=" + rowId, null, null, null, null);
	}
	public Cursor getAllSemester() {
		return database.query(DATABASE_NAME, new String[] {KEY_FIRSTSEMESTER,
				KEY_SECONDSEMESTER}, null, null, null, null, null);
	}
	public Cursor getQuarter(long rowId) {
		return database.query(DATABASE_NAME, new String[] {KEY_FIRSTQUARTER,
				KEY_SECONDQUARTER, KEY_THIRDQUARTER}, KEY_ROWID + "=" + rowId, null, null, null, null);
	}
	public Cursor getAllQuarter() {
		return database.query(DATABASE_NAME, new String[] {KEY_FIRSTQUARTER,
				KEY_SECONDQUARTER, KEY_THIRDQUARTER}, null, null, null, null, null);
	}
	public Cursor getAll() {
		return database.query(DATABASE_NAME, new String[] {KEY_ROWID, KEY_QUARTER_FLAG,
				KEY_FIRSTSEMESTER, KEY_SECONDSEMESTER, KEY_FIRSTQUARTER, KEY_SECONDQUARTER,
				KEY_THIRDQUARTER, KEY_COMPLETE}, null, null, null, null, null);
	}
	public class CourseRequirementsDatabaseHelper extends SQLiteOpenHelper {
		CourseRequirementsDatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE " + DATABASE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
					KEY_QUARTER_FLAG + " TEXT, " + KEY_FIRSTSEMESTER + " TEXT, " + KEY_SECONDSEMESTER + " TEXT, " +
					KEY_FIRSTQUARTER + " TEXT, " + KEY_SECONDQUARTER + " TEXT, " + KEY_THIRDQUARTER + " TEXT, " +
					KEY_COMPLETE + " TEXT);");	
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion,
				int newVersion) {				
		}
		
	}
	public ContentValues createInitialValues(int i) {
		ContentValues values = new ContentValues();
		values.put(KEY_ROWID, i);
		values.put(KEY_QUARTER_FLAG, "false");
		values.put(KEY_FIRSTSEMESTER, "0");
		values.put(KEY_SECONDSEMESTER, "0");
		values.put(KEY_FIRSTQUARTER, "0");
		values.put(KEY_SECONDQUARTER, "0");
		values.put(KEY_THIRDQUARTER, "0");
		values.put(KEY_COMPLETE, "false");
		return values;
	}
	public ContentValues createQuarterValues(String first, String second, String third) {
		ContentValues values = new ContentValues();
		values.put(KEY_FIRSTQUARTER, first);
		values.put(KEY_SECONDQUARTER, second);
		values.put(KEY_THIRDQUARTER, third);
		return values;
	}
	public ContentValues createSemesterValues(String first, String second) {
		ContentValues values = new ContentValues();
		values.put(KEY_FIRSTSEMESTER, first);
		values.put(KEY_SECONDSEMESTER, second);
		return values;
	}
}

